﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbEnClassDal
	{
		public DbEnClassDal()
		{
		}
 		#region Insert
 		public static int Insert(DbEnClass dbEnClass)
		{	
			string sql = @"Insert Into DbEnClass (
					CId,
					CEnName,
					CEnNote,
					CEnKeywords,
					CEnDescription,
					CEnAddTime
				) values({0},'{1}','{2}','{3}','{4}','{5}')";
			sql = string.Format(sql,
					dbEnClass.CId,
					SqlHelper.String(dbEnClass.CEnName),
					SqlHelper.String(dbEnClass.CEnNote),
					SqlHelper.String(dbEnClass.CEnKeywords),
					SqlHelper.String(dbEnClass.CEnDescription),
					dbEnClass.CEnAddTime
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbEnClass dbEnClass)
		{	
			string sql = @"Update DbEnClass set 

					CEnName= '{0}',
					CEnNote= '{1}',
					CEnKeywords= '{2}',
					CEnDescription= '{3}',
					CEnAddTime= '{4}'
				where

					CId= {5}";
			sql = string.Format(sql,
					SqlHelper.String(dbEnClass.CEnName),
					SqlHelper.String(dbEnClass.CEnNote),
					SqlHelper.String(dbEnClass.CEnKeywords),
					SqlHelper.String(dbEnClass.CEnDescription),
					dbEnClass.CEnAddTime,
					dbEnClass.CId);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 cId)
		{
		
			string sql = @"Delete from DbEnClass where 
					CId= {0}";
			sql = string.Format(sql,
					cId
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbEnClass a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbEnClass Select(Int32 cId)
		{
			DbEnClass dbEnClass = null;
			string sql = @"select a.* from DbEnClass a  where
					a.CId= {0}
";
			sql = string.Format(sql,
					cId
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbEnClass = new DbEnClass(ds.Tables[0].Rows[0]);					
			return dbEnClass;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbEnClass a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbEnClass a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbEnClass a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
